﻿/**
 *
 * @title: Basic MVC Framework, Model Class
 * @author: 
 * @license: 
 * @updated: 29/07/2010
 *
 * @version	1.0 
 */

package com.mediagallary.model
{
	import flash.events.EventDispatcher;
	import flash.events.Event;
	
	//import com.mediagallary.model.*;
	import com.mediagallary.utils.Xmlloader;
	import com.mediagallary.utils.XmlloaderEvent;
	

	/**
	 * Basic Model class in Model/View/Controller triad.
	 */
	public class Model extends EventDispatcher
	{	
	
		public static const UPDATE:String = "modelUpdated";
		public static const MODEL_CHANGE:String = "modelChange";
		public static const MODEL_LOADED:String = "modelLoaded";
		
		public var gallaryData:*;
		private var xmlloader:Xmlloader;
		public var currentIndex:int = 0;
		public var totalItems:int;
		
		public function Model()
		{
			trace("Model")
			xmlloader = new Xmlloader()
			xmlloader.addEventListener(XmlloaderEvent.COMPLETED, dataLoadHandler);
		}
		
		public function loadData(path:String):void
		{
			xmlloader.loadXml(path);
		}
		
		protected function dataLoadHandler(event:XmlloaderEvent):void
		{
			trace("Model dataLoadHandler : ")
			//trace("Model dataLoadHandler : "+event.XMLData)
			gallaryData = event.XMLData;
			//var e:CustomEvent = new CustomEvent(Model.UPDATE, true, false, ob);
			//dispatchEvent(e);
			/*var e:Event = new Event(Model.MODEL_CHANGE, true, false);
			dispatchEvent(e);*/
			setTotalItem();
			updateData();
		}
		
		public function setTotalItem():void
		{
			totalItems = gallaryData.item.length();
			trace("setTotalItem totalItems : "+totalItems);
			var e:Event = new Event(Model.MODEL_CHANGE, true, false);
			dispatchEvent(e);
			//dispatchEvent(new Event(Model.MODEL_CHANGE));
		}
		
		protected function updateData():void
		{
			/*trace("updateData");
			totalItems = gallaryData.length();
			trace("updateData totalItems : "+totalItems);*/
		}
		
		
		public function setCurrectMediaItem(index:int):void
		{
			currentIndex = index;
			//updateData();
			var e:Event = new Event(Model.UPDATE, true, false);
			dispatchEvent(e);
		}
		
		// keep currentIndex in bounds:
		/*private function manageCurrentIndex():void
		{
			if(currentIndex >= totalItems)
			{
				currentIndex = 0;
			}
			else if(currentIndex < 0)
			{
				currentIndex = totalItems -1;
			}
		}
		
		public function setCurrentIndex(index:int)
		{
			currentIndex = index;
			 manageCurrentIndex();
			updateData();
			dispatchEvent(new Event(Model.MODEL_CHANGE));
		}
		
	 	protected function dataLoaded(event:Event):void
		{
			data = loader.data;
			setCurrentIndex(currentIndex);
		}*/
		
		
		/**
		 * This method will cause an update event and object
		 * to be passed up to the view. Using CustomEvent allows
		 * a data object to be passed along with the event
		 */
		/*public function onUpdate(ob:Object):void 
		{
			var e:CustomEvent = new CustomEvent(AbstractModel.UPDATE, true, false, ob);
			dispatchEvent(e);
		}*/
		
	}
}